********************************************************************************
*** Voting for Populism in Europe Replication Files
*** Table A3: Summary Statistics (MI Data)
*** Table A4: MI Estimate of Correlations
***
*** Required data files: "imputed_econdata_voteshare_merged.dta"
***                     
*** 
*** Created by: Thomas Cunningham
*** Date: 5 January 2021
******************************************************************

clear all
version 16.1
cd ..
 	
use "data/imputed/imputed_econdata_voteshare_merged.dta", clear

misum shock_china_ind_L1 shock_lowwage_ind_L1  shock_fdi_in_ind_L1 shock_robots_mfg_L1 rti_region_L1 reg_ict_emp_mfg_L1 shock_fgn92_L1, format(%9.3f)


**** MI correlation ******************/
cap program drop ecorr
program ecorr, eclass
	version 11
	syntax [varlist] [if] [in] [aw fw] [, * ]
	if (`"`weight'"'!="") {
		local wgt `weight'`exp'
	}
	marksample touse
	correlate `varlist' `if' `in' `wgt', `options'
	tempname b V
	mata: st_matrix("`b'", vech(st_matrix("r(C)"))')
	local p = colsof(`b')
	mat `V' = J(`p',`p',0)
	local cols: colnames `b'
	mat rownames `V' = `cols'
	eret post `b' `V' [`wgt'] , obs(`=r(N)') esample(`touse')
	eret local cmd ecorr
	eret local title "Lower-diagonal correlation matrix"
	eret local vars "`varlist'"

end

cap program drop micorr
program micorr, rclass
	tempname esthold
	_estimates hold `esthold', nullok restore
	qui mi estimate, cmdok: ecorr `0'
	tempname C_mi
	mata: st_matrix("`C_mi'", invvech(st_matrix("e(b_mi)")'))
	mat colnames `C_mi' = `e(vars)'
	mat rownames `C_mi' = `e(vars)'
	di
	di as txt "Multiple-imputation estimate of the correlation matrix"	
	di as txt "(obs=" string(e(N_mi),"%9.0g") ")"
	matlist `C_mi'
	return clear
	ret matrix C_mi = `C_mi'
end	



* MI CORRELATION
micorr shock_china_ind_L1 shock_lowwage_ind_L1 shock_fdi_in_ind_L1 shock_robots_mfg_L1 rti_region_L1 shock_fgn92_L1 

